#include "SQLDropTableStatement.h"
#include "../../../DbType.h"
#include "../SQLCommentHint.h"
#include "../SQLExpr.h"
#include "../SQLName.h"
#include "../SQLObject.h"
#include "../../visitor/SQLASTVisitor.h"
#include "../../../utils/listTran.h"
#include "../../../utils/listAddAll.h"
#include "SQLExprTableSource.h"

SQLDropTableStatement::SQLDropTableStatement()
{
  hints = std::make_shared<std::list<SQLCommentHint_ptr>>();
  tableSources = std::make_shared<std::list<SQLExprTableSource_ptr>>();
  this->tableSources = std::make_shared<std::list<SQLExprTableSource_ptr>>();
}

SQLDropTableStatement::SQLDropTableStatement(DbType_ptr  dbType)
    : SQLStatementImpl(dbType)
{
  hints = std::make_shared<std::list<SQLCommentHint_ptr>>();
  tableSources = std::make_shared<std::list<SQLExprTableSource_ptr>>();
  this->tableSources = std::make_shared<std::list<SQLExprTableSource_ptr>>();
}

SQLDropTableStatement::SQLDropTableStatement(SQLName_ptr name, DbType_ptr  dbType)
    : SQLDropTableStatement(SQLExprTableSource_ptr(new SQLExprTableSource(name)), dbType)
{
}

SQLDropTableStatement::SQLDropTableStatement(SQLName_ptr name)
    : SQLDropTableStatement(name, DbType_ptr(new DbType(&DbType::other)))
{
}

SQLDropTableStatement::SQLDropTableStatement(SQLExprTableSource_ptr tableSource)
    : SQLDropTableStatement(tableSource, DbType_ptr(new DbType(&DbType::other)))
{
}

SQLDropTableStatement::SQLDropTableStatement(SQLExprTableSource_ptr tableSource, DbType_ptr  dbType)
    : SQLDropTableStatement(dbType)
{
  this->tableSources = std::make_shared<std::list<SQLExprTableSource_ptr>>();
  this->tableSources->push_back(tableSource);
}

SQLExprTableSource_list_ptr SQLDropTableStatement::getTableSources()
{
  return tableSources;
}

void SQLDropTableStatement::addPartition(SQLExprTableSource_ptr tableSource)
{
  if (tableSource != NULL)
  {
    tableSource->setParent(SharedObject(SQLDropTableStatement));
  }
  this->tableSources->push_back(tableSource);
}

void SQLDropTableStatement::setName(SQLName_ptr name)
{
  this->addTableSource(SQLExprTableSource_ptr(new SQLExprTableSource(name)));
}

void SQLDropTableStatement::addTableSource(SQLName_ptr name)
{
  this->addTableSource(SQLExprTableSource_ptr(new SQLExprTableSource(name)));
}

void SQLDropTableStatement::addTableSource(SQLExprTableSource_ptr tableSource)
{
  tableSources->push_back(tableSource);
}

void SQLDropTableStatement::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLDropTableStatement)))
  {
    SQLObject_list_ptr tmp = ListTran::tran_class<SQLExprTableSource, SQLObject>(tableSources);
    this->acceptChild(visitor, tmp);
  }
  visitor->endVisit(SharedObject(SQLDropTableStatement));
}

SQLExprTableSource_list_ptr SQLDropTableStatement::getChildren()
{
  return this->tableSources;
}

BOOL_ptr SQLDropTableStatement::isPurge()
{
  return purge;
}

void SQLDropTableStatement::setPurge(BOOL_ptr purge)
{
  this->purge = purge;
}

BOOL_ptr SQLDropTableStatement::isIfExists()
{
  return ifExists;
}

void SQLDropTableStatement::setIfExists(BOOL_ptr ifExists)
{
  this->ifExists = ifExists;
}

BOOL_ptr SQLDropTableStatement::isCascade()
{
  return cascade;
}

void SQLDropTableStatement::setCascade(BOOL_ptr cascade)
{
  this->cascade = cascade;
}

BOOL_ptr SQLDropTableStatement::isRestrict()
{
  return restrict;
}

void SQLDropTableStatement::setRestrict(BOOL_ptr restrict)
{
  this->restrict = restrict;
}

BOOL_ptr SQLDropTableStatement::isTemporary()
{
  return temporary;
}

void SQLDropTableStatement::setTemporary(BOOL_ptr temporary)
{
  this->temporary = temporary;
}

BOOL_ptr SQLDropTableStatement::isExternal()
{
  return external;
}

void SQLDropTableStatement::setExternal(BOOL_ptr external)
{
  this->external = external;
}

SQLCommentHint_list_ptr SQLDropTableStatement::getHints()
{
  return hints;
}

void SQLDropTableStatement::setHints(SQLCommentHint_list_ptr hints)
{
  this->hints = hints;
}

BOOL_ptr SQLDropTableStatement::isDropPartition()
{
  return isDropPartition_;
}

void SQLDropTableStatement::setDropPartition(BOOL_ptr dropPartition)
{
  isDropPartition_ = dropPartition;
}

SQLExpr_ptr SQLDropTableStatement::getWhere()
{
  return where;
}

void SQLDropTableStatement::setWhere(SQLExpr_ptr x)
{
  if (x != NULL)
  {
    // SQLObject_ptr tmp = static_cast<SQLObject_ptr >(static_cast<Object_ptr >(this));
    x->setParent(SharedObject(SQLDropTableStatement));
  }
  this->where = x;
}

SQLDDLStatement::DDLObjectType SQLDropTableStatement::getDDLObjectType()
{
  return SQLDDLStatement::DDLObjectType::TABLE;
}

SQLName_ptr SQLDropTableStatement::getName()
{
  if (tableSources->size() == 1)
  {
    auto dataSource0 = tableSources->begin();
    return (*dataSource0)->getName();
  }
  return NULL;
}
